 package com.example.myapplication.teenager;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.Hostel_DetailBean;
import com.example.myapplication.apter.PhotoViewPagerApter;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

 public class Hostel_DetailActivity extends AppCompatActivity {

     private String dataBeanID;
     private OkHttpClient okHttpClient;
     private Gson gson;
     private Request request;
     private Handler handler;
     private Button backButton;
     private ViewPager photoViewPager;
     private TextView addressTextView;
     private TextView contactTextView;
     private TextView checkInTimeTextView;
     private TextView bedsTextView;
     private TextView introductionTextView;
     private TextView roomTextView;
     private TextView facilitiesTextView;
     private TextView servicesTextView;
     private TextView bedsCountGirlTextView;
     private Hostel_DetailBean hostel_detailBean;
     private List<String> list_img ;

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hostel__detail);
        Intent intent = getIntent();
        dataBeanID = intent.getStringExtra("RowsBeanID");
        initview();
        sendbean();
    }

     private void sendbean() {
         request = new Request.Builder()
                 .url("http://124.93.196.45:10001/prod-api/api/youth-inn/youth-inn/"+dataBeanID)
                 .addHeader("Authorization", TokenBean.token)
                 .build();
         okHttpClient.newCall(request).enqueue(new Callback() {
             @Override
             public void onFailure(@NotNull Call call, @NotNull IOException e) {

             }

             @Override
             public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                 String string = response.body().string();
                 hostel_detailBean = gson.fromJson(string, Hostel_DetailBean.class);
                 Log.d("hostel_detailBean",string);
                 handler.post(new Runnable() {
                     @Override
                     public void run() {
                         addressTextView.setText("驿站地址:"+hostel_detailBean.getData().getAddress());
                         contactTextView.setText("联系电话:"+hostel_detailBean.getData().getPhone());
                         checkInTimeTextView.setText("办理入住时间段:"+hostel_detailBean.getData().getWorkTime());
                         bedsTextView.setText("剩余床位数：男:"+String.valueOf(hostel_detailBean.getData().getBedsCountBoy()));
                         bedsCountGirlTextView.setText("剩余床位数：女:"+String.valueOf(hostel_detailBean.getData().getBedsCountGirl()));
                         introductionTextView.setText("驿站简介:"+hostel_detailBean.getData().getIntroduce());
                         roomTextView.setText("房间配置:"+hostel_detailBean.getData().getInternalFacilities());
                         facilitiesTextView.setText("周边配套:"+hostel_detailBean.getData().getSurroundingFacilities());
                         servicesTextView.setText("特色服务:"+hostel_detailBean.getData().getSpecialService());

                         PhotoViewPagerApter photoViewPagerApter = new PhotoViewPagerApter(Hostel_DetailActivity.this,hostel_detailBean.getData().getImageUrlList());
                         photoViewPager.setAdapter(photoViewPagerApter);
                         mHandler.sendEmptyMessageDelayed(0, 3 * 500);
                     }
                 });
             }
         });
     }

     private void initview() {
         okHttpClient = new OkHttpClient();
         gson = new Gson();
         handler = new Handler();
         list_img = new ArrayList<>();

         backButton = findViewById(R.id.backButton);
         photoViewPager = findViewById(R.id.photoViewPager);
         addressTextView = findViewById(R.id.addressTextView);
         contactTextView = findViewById(R.id.contactTextView);
         checkInTimeTextView = findViewById(R.id.checkInTimeTextView);
         bedsCountGirlTextView = findViewById(R.id.bedsCountGirlTextView);
         bedsTextView = findViewById(R.id.bedsTextView);
         introductionTextView = findViewById(R.id.introductionTextView);
         roomTextView = findViewById(R.id.roomTextView);
         facilitiesTextView = findViewById(R.id.facilitiesTextView);
         servicesTextView = findViewById(R.id.servicesTextView);


     }

     private Handler mHandler = new Handler() {
         @Override
         public void handleMessage(@NonNull Message msg) {
             super.handleMessage(msg);

             if (msg.what == 0) {
                 int index = photoViewPager.getCurrentItem();
                 index = (index + 1) % 7;
                 photoViewPager.setCurrentItem(index);
                 mHandler.sendEmptyMessageDelayed(0, 3 * 500);
             }
         }
     };
 }